<?php

namespace App\Http\Controllers\Wechat;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Ad\Advert;
use App\Models\Shop\Product;

class HomeController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_index' => 'on',
        ]);
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 商城前端---首页
     */
    public function index(){
//        session()->flush();
//        return session('wechat.oauth_user.original');

        //首页焦点图
        $sliders = Advert::where('category_id', 1)->orderBy('sort_order')->get();
        //首页banner图
        $banners = Advert::where('category_id', 2)->limit(3)->get();
        //商品列表
        $products = Product::where('is_onsale', 1)->limit(3)->get();

        $total = Product::count();
        return view('wechat.index',compact('sliders','banners','products','total'));
    }

    /**
     * @param Request $request
     * @return mixed
     * 下拉加载更多
     */
    public function scroll(Request $request)
    {
        $start = $request->start;

        $list = Product::where('is_onsale', true)->orderBy('is_top', 'desc')->offset($start)->limit(3)->get();
        return $list;
    }

}
